using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 权限实体
/// </summary>
[Table(Name = "permissions")]
[Index("idx_permission_code", "PermissionCode", true)] // 唯一索引
public class Permission
{
    /// <summary>
    /// 权限 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 权限代码（唯一标识）
    /// 例如：workflow.create、workflow.view、user.manage
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string PermissionCode { get; set; } = null!;

    /// <summary>
    /// 权限名称
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string PermissionName { get; set; } = null!;

    /// <summary>
    /// 权限描述
    /// </summary>
    [Column(StringLength = 1000)]
    public string? Description { get; set; }

    /// <summary>
    /// 权限分组（用于 UI 分类显示）
    /// 例如：Workflow、User、Node、System
    /// </summary>
    [Column(StringLength = 100)]
    public string? Category { get; set; }

    /// <summary>
    /// 资源类型
    /// 例如：API、Menu、Button、Data
    /// </summary>
    [Column(StringLength = 50)]
    public string? ResourceType { get; set; }

    /// <summary>
    /// 资源标识
    /// 例如：/api/workflow/*、/api/user/*
    /// </summary>
    [Column(StringLength = 500)]
    public string? ResourceIdentifier { get; set; }

    /// <summary>
    /// 是否系统权限（系统权限不可删除）
    /// </summary>
    [Column(IsNullable = false)]
    public bool IsSystem { get; set; } = false;

    /// <summary>
    /// 排序
    /// </summary>
    [Column(IsNullable = false)]
    public int SortOrder { get; set; } = 0;

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 创建者
    /// </summary>
    [Column(StringLength = 100)]
    public string? CreatedBy { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdatedAt { get; set; }

    /// <summary>
    /// 拥有该权限的角色
    /// </summary>
    [Navigate(ManyToMany = typeof(RolePermission))]
    public List<Role> Roles { get; set; } = new();
}
